/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JP_RegistrarProgramaCorte.java
 *
 * Created on 04-nov-2009, 10:03:32
 */

package gui;

import controlacciones.ejb3.pojo.TableBeanBinding_DatosArchivoProgramaCorte;
import controlaccionescomercial.ControlAccionesComercialView;
import delegate.GestionDelegate;
import helper.JP_Modelo;
import helper.Utiles_MensajesDialogo;
import helper.Utiles_Panel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JInternalFrame;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;

/**
 *
 * @author Vanessa
 */
public class JP_RegistrarProgramaCorte extends JP_Modelo {

    /** Creates new form JP_RegistrarProgramaCorte */
    public JP_RegistrarProgramaCorte() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bg = new javax.swing.ButtonGroup();
        jp_Datos = new javax.swing.JPanel();
        rb_ArchProcesados = new javax.swing.JRadioButton();
        rb_ArchPendientes = new javax.swing.JRadioButton();
        btnConsultar = new javax.swing.JButton();
        jp_Detalle = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbData = new javax.swing.JTable();
        btnProcesar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        btnDetalle = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class).getContext().getResourceMap(JP_RegistrarProgramaCorte.class);
        jp_Datos.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_Datos.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_Datos.border.titleFont"))); // NOI18N
        jp_Datos.setName("jp_Datos"); // NOI18N

        bg.add(rb_ArchProcesados);
        rb_ArchProcesados.setText(resourceMap.getString("rb_ArchProcesados.text")); // NOI18N
        rb_ArchProcesados.setName("rb_ArchProcesados"); // NOI18N
        rb_ArchProcesados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_ArchProcesadosActionPerformed(evt);
            }
        });

        bg.add(rb_ArchPendientes);
        rb_ArchPendientes.setFont(resourceMap.getFont("rb_ArchPendientes.font")); // NOI18N
        rb_ArchPendientes.setSelected(true);
        rb_ArchPendientes.setText(resourceMap.getString("rb_ArchPendientes.text")); // NOI18N
        rb_ArchPendientes.setName("rb_ArchPendientes"); // NOI18N
        rb_ArchPendientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_ArchPendientesActionPerformed(evt);
            }
        });

        btnConsultar.setFont(resourceMap.getFont("btnConsultar.font")); // NOI18N
        btnConsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/find.gif"))); // NOI18N
        btnConsultar.setText(resourceMap.getString("btnConsultar.text")); // NOI18N
        btnConsultar.setName("btnConsultar"); // NOI18N
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_DatosLayout = new javax.swing.GroupLayout(jp_Datos);
        jp_Datos.setLayout(jp_DatosLayout);
        jp_DatosLayout.setHorizontalGroup(
            jp_DatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DatosLayout.createSequentialGroup()
                .addComponent(rb_ArchPendientes)
                .addGap(18, 18, 18)
                .addComponent(rb_ArchProcesados)
                .addGap(18, 18, 18)
                .addComponent(btnConsultar)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jp_DatosLayout.setVerticalGroup(
            jp_DatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(rb_ArchPendientes)
                .addComponent(rb_ArchProcesados)
                .addComponent(btnConsultar))
        );

        jp_Detalle.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_Detalle.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_Detalle.border.titleFont"))); // NOI18N
        jp_Detalle.setName("jp_Detalle"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tbData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbData.setName("tbData"); // NOI18N
        jScrollPane1.setViewportView(tbData);

        javax.swing.GroupLayout jp_DetalleLayout = new javax.swing.GroupLayout(jp_Detalle);
        jp_Detalle.setLayout(jp_DetalleLayout);
        jp_DetalleLayout.setHorizontalGroup(
            jp_DetalleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
        );
        jp_DetalleLayout.setVerticalGroup(
            jp_DetalleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class).getContext().getActionMap(JP_RegistrarProgramaCorte.class, this);
        btnProcesar.setAction(actionMap.get("procesarProgramaCorte")); // NOI18N
        btnProcesar.setFont(resourceMap.getFont("btnProcesar.font")); // NOI18N
        btnProcesar.setText(resourceMap.getString("btnProcesar.text")); // NOI18N
        btnProcesar.setName("btnProcesar"); // NOI18N

        btnCerrar.setFont(resourceMap.getFont("btnCerrar.font")); // NOI18N
        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/fife/plaf/Office2003/close.gif"))); // NOI18N
        btnCerrar.setText(resourceMap.getString("btnCerrar.text")); // NOI18N
        btnCerrar.setName("btnCerrar"); // NOI18N
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnDetalle.setFont(resourceMap.getFont("btnDetalle.font")); // NOI18N
        btnDetalle.setText(resourceMap.getString("btnDetalle.text")); // NOI18N
        btnDetalle.setName("btnDetalle"); // NOI18N
        btnDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(185, Short.MAX_VALUE)
                .addComponent(btnDetalle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnProcesar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCerrar)
                .addGap(19, 19, 19))
            .addComponent(jp_Detalle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jp_Datos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jp_Datos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_Detalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCerrar)
                    .addComponent(btnProcesar)
                    .addComponent(btnDetalle))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        if(this.rb_ArchPendientes.isSelected()){
            this.listaDatos = GestionDelegate.getInstance().getListaArchivosPendientes();
            this.cargaTabla();
        }else if( this.rb_ArchProcesados.isSelected() ){
            this.listaDatos = GestionDelegate.getInstance().getListaArchivosProcesados();
            this.cargaTabla();
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.cerrarPadre();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void rb_ArchProcesadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_ArchProcesadosActionPerformed
        this.btnProcesar.setEnabled(false);
    }//GEN-LAST:event_rb_ArchProcesadosActionPerformed

    private void rb_ArchPendientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_ArchPendientesActionPerformed
        this.btnProcesar.setEnabled(true);
    }//GEN-LAST:event_rb_ArchPendientesActionPerformed

    private void btnDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleActionPerformed
        // TODO add your handling code here:
        consultarProgCort();
    }//GEN-LAST:event_btnDetalleActionPerformed

    private void cargaTabla(){        
        try{
            this.bindingGroup.removeBinding(jTableBinding);
        }catch( Exception e ){
        }

        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaDatos, this.tbData);
        
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ind}"));
        columnBinding.setColumnName("Procesado");
        columnBinding.setColumnClass(Boolean.class);
        if (this.rb_ArchPendientes.isSelected()) {
            columnBinding.setEditable(true);
        } else if( this.rb_ArchProcesados.isSelected() ){
            columnBinding.setEditable(false);
        }
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding1 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fileName}"));
        columnBinding1.setColumnName("Archivo");
        columnBinding1.setColumnClass(String.class);
        columnBinding1.setEditable(false);


        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding2 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fecha}"));
        columnBinding2.setColumnName("Fecha");
        columnBinding2.setColumnClass(String.class);
        columnBinding2.setEditable(false);

        bindingGroup.addBinding(jTableBinding);
        bindingGroup.bind();
    }

    public void consultarProgCort(){
        int[] selected = this.tbData.getSelectedRows();
        if( selected.length > 1){
            helper.Utiles_MensajesDialogo.mensajeAlerta( "Error: Seleccione solo un archivo para mostrar su detalle" );
            return;
        }else{
            if( selected.length < 1){
                helper.Utiles_MensajesDialogo.mensajeAlerta( "Error: Seleccione un archivo para mostrar su detalle" );
                return;
            }
        }
        List< TableBeanBinding_DatosArchivoProgramaCorte> toRemove = new ArrayList<TableBeanBinding_DatosArchivoProgramaCorte>(selected.length);
        for (int idx=0; idx<selected.length; idx++) {
            TableBeanBinding_DatosArchivoProgramaCorte t = this.listaDatos.get(tbData.convertRowIndexToModel(selected[idx]));
            if( t != null){
                JP_DetalleCorte panel = new JP_DetalleCorte(  );
                    if( ifrConsultarDetalleCortes == null || ifrConsultarDetalleCortes.isClosed() ){
                        ifrConsultarDetalleCortes = Utiles_Panel.creaVentanaInterna_Panel((ControlAccionesComercialView)this.getRef(), panel, "Detalle del Programa de Cortes");
                        panel.setDatos(t.getFileName());
                        panel.setPadre(Utiles_Panel.TIPO_JINTERNALFRAME, ifrConsultarDetalleCortes);
                        panel.setRef( this );
                    }else{
                        JP_DetalleCorte panel_detalle = (JP_DetalleCorte)ifrConsultarDetalleCortes.getPanel();
                        panel_detalle.setDatos(t.getFileName());
                    }
                }    
        }
    }

    @Action
    public Task procesarProgramaCorte() {
        return new ProcesarProgramaCorte(org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class));
    }

    private class ProcesarProgramaCorte extends org.jdesktop.application.Task<Object, Void> {
        ProcesarProgramaCorte(org.jdesktop.application.Application app) {
            super(app);
        }
        @Override protected Object doInBackground() {
            btnConsultar.setEnabled(false);
            btnProcesar.setEnabled(false);
            List listaTemp = new ArrayList(1);
            for (TableBeanBinding_DatosArchivoProgramaCorte obj : listaDatos) {
                if(obj.isInd()){
                    listaTemp.add(obj);
                }
            }
            GestionDelegate.getInstance().procesarProgramasCorte(listaTemp);            
            return null;  // return your result
        }
        @Override protected void succeeded(Object result) {
            btnConsultar.setEnabled(true);
            btnProcesar.setEnabled(true);
            btnConsultar.doClick();
            Utiles_MensajesDialogo.mensajeExito();
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bg;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnDetalle;
    private javax.swing.JButton btnProcesar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jp_Datos;
    private javax.swing.JPanel jp_Detalle;
    private javax.swing.JRadioButton rb_ArchPendientes;
    private javax.swing.JRadioButton rb_ArchProcesados;
    private javax.swing.JTable tbData;
    // End of variables declaration//GEN-END:variables

    private org.jdesktop.swingbinding.JTableBinding jTableBinding;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    private List<TableBeanBinding_DatosArchivoProgramaCorte> listaDatos;
    private helper.JInternalFrame_Modelo ifrConsultarDetalleCortes;
}
